/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Scanner;
import ua.yandex.MyList.MyLinkedList;

/**
 *
 * @author Brothers
 */
public class AccountingSystem {

    //private MyList list;
    private int idCounter = 0;
    private Calendar date;
    private MyLinkedList turn;

    public AccountingSystem() {
        // this.list = new MyLinkedList();
        this.idCounter = 0;
        this.date.getInstance();
        this.turn = new MyLinkedList();
    }

    public int getTurnstileId(int number) {
        if (this.turn.size() <= number) {
            return ((Turnstile) (this.turn.get(number))).getId();
        } else {
            return 0;
        }
    }

    public Calendar getDate() {
        //return this.date;
        return this.date;
    }

    public int getIdCounter() {
        return this.idCounter;
    }

    public MyLinkedList getTurn() {
        return this.turn;
    }

    public void setDate(Calendar data) {
        this.date.setTime(data.getTime());
    }

    public SkiPass createSkiPass(Enums.Week temp) {
        this.idCounter++;
        SkiPass pass;
        Scanner in = new Scanner(System.in);
        if (temp != Enums.Week.None) {
            System.out.println("1 - Lift, 2 - Day");
            int i = in.nextInt();
            if (i == 1) {
                pass = createLift(this.date, temp);
            } else {
                pass = createDay(this.date, temp);
            }
        } else {
            pass = createSeason(this.date, temp);
        }
        in.close();
        return pass;
    }

    public PassNumberLift createLift(
            Calendar firstDatePass, Enums.Week ogrPass) {
        PassNumberLift temporary = new PassNumberLift(this.idCounter,
                Enums.PassType.Work, firstDatePass, ogrPass);
        int i;
        Scanner in = new Scanner(System.in);
        System.out.print("1 - 10, 2 - 20, 3 - 50, 4 - 100");
        i = in.nextInt();
        temporary.setNumberLift(i - 1);
        in.close();
        return temporary;
    }

    public PassForSeason createSeason(
            Calendar firstDatePass, Enums.Week ogrPass) {
        PassForSeason temporary = new PassForSeason(this.idCounter,
                Enums.PassType.Season, firstDatePass, ogrPass);
        temporary.setNumberDay(1);
        return temporary;
    }

    public PassForAnDay createDay(
            Calendar firstDatePass, Enums.Week ogrPass) {
        PassForAnDay temporary = new PassForAnDay(this.idCounter,
                Enums.PassType.Work, firstDatePass, ogrPass);

        int i;
        Scanner in = new Scanner(System.in);
        System.out.print("1 - 20, 2 - 10, 3 - 5, 4 - 3, 5 - 2, 6 - 1");
        i = in.nextInt();
        temporary.setNumberDay(i);
        in.close();
        return temporary;
    }

    public boolean wrongDay(SkiPass pass) {
        return pass.isDayInOgr(this.date);
    }

    public boolean checkPass(SkiPass pass, int id) {
        if (!pass.getPassAccess() && this.wrongDay(pass)) {
            ((Turnstile) this.turn.get(id)).plusAccessFalse();
            return false;
        }
        return isPassRight(pass, id);
    }

    public boolean isPassRight(SkiPass pass, int id) {
        boolean access;
        if (pass instanceof PassForSeason && !(pass instanceof PassForAnDay)) {
            access = ((PassForSeason) pass).isPassForSeasonRight(this, id);
            if (!access) {
                return false;
            }
        } else if (pass instanceof PassNumberLift) {
            access = ((PassNumberLift) pass).isPassForSeasonRight(this, id);
            if (!access) {
                return false;
            }
        } else if (pass instanceof PassForAnDay) {
            access = ((PassForAnDay) pass).isPassForAnDayRight(this, id);
            if (!access) {
                return false;
            }
        }
        if (pass instanceof PassNumberLift) {
            ((PassNumberLift) pass).setNumberLift(
                    ((PassNumberLift) pass).getNumberLift() - 1);
        }
        ((Turnstile) this.getTurn()
                .get(id)).plusAccessTrue();
        return true;
    }

    public Turnstile createTurnstile() {

        Turnstile temp = new Turnstile(this);
        return temp;
    }

    public void addToTurn(Turnstile trn) {
        this.turn.add(trn);
    }

    public int getAllAccessTrue() {
        MyLinkedList temp = this.turn;
        int number = 0;
        while (!temp.isEmpty()) {
            number += ((Turnstile) (temp.getFirst())).getAllaccessTrue();
        }
        return number;
    }

    public int getAllAccessFalse() {
        MyLinkedList temp = this.turn;
        int number = 0;
        while (!temp.isEmpty()) {
            number += ((Turnstile) (temp.getFirst())).getAllaccessFalse();
        }
        return number;
    }

}
